
#include "hi_ext_util.h"
#include "plug_detectmng.h"
#include "hisignalling.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* End of #ifdef __cplusplus */

typedef struct PLUG_DETECTMNG_DETECTINFO_s
{
    HI_S32 s32Class;
    HI_U64 u64DetectTime;
//    HI_BOOL bDetected;
}PLUG_DETECTMNG_DETECTINFO_S;

typedef struct PLUG_DETECTMNG_CTX_s
{
    HI_BOOL bInit;
    PLUG_DETECTMNG_DETECTINFO_S *pastInfo;
    PLUG_DETECTMNG_CFG_S stMngCfg;
}PLUG_DETECTMNG_CTX_S;

static PLUG_DETECTMNG_CTX_S s_stDetectMngCtx = 
{
    .bInit = HI_FALSE
};

HI_S32 PLUG_DETECTMNG_Init(PLUG_DETECTMNG_CFG_S *pstMngCfg)
{
    if(s_stDetectMngCtx.bInit)
    {
        LOGI("DetectMng inited, return HI_SUCCESS\n");
        return HI_SUCCESS;
    }

    s_stDetectMngCtx.stMngCfg.s32ClassNum = pstMngCfg->s32ClassNum;
    s_stDetectMngCtx.stMngCfg.s32DetectIntervalMs = pstMngCfg->s32DetectIntervalMs;
    s_stDetectMngCtx.pastInfo = (PLUG_DETECTMNG_DETECTINFO_S*)malloc(sizeof(PLUG_DETECTMNG_DETECTINFO_S) * s_stDetectMngCtx.stMngCfg.s32ClassNum);
    if (HI_NULL == s_stDetectMngCtx.pastInfo)
    {
        LOGE("s_stDetectMngCtx.pastInfo malloc failed, size[%d]\n",
            sizeof(PLUG_DETECTMNG_DETECTINFO_S) * s_stDetectMngCtx.stMngCfg.s32ClassNum);
        return HI_FAILURE;
    }

    HI_S32 s32Idx = 0;
    for(s32Idx = 0; s32Idx < s_stDetectMngCtx.stMngCfg.s32ClassNum; s32Idx++)
    {
        s_stDetectMngCtx.pastInfo[s32Idx].s32Class = s32Idx + 1;
        s_stDetectMngCtx.pastInfo[s32Idx].u64DetectTime = 0;
    }
    s_stDetectMngCtx.bInit = HI_TRUE;
    return HI_SUCCESS;
}

HI_BOOL PLUG_DETECTMNG_IsDetectInInterval(HI_S32 s32Class)
{
    if(!s_stDetectMngCtx.bInit)
    {
        LOGE("DetectMng isn`t inited, return HI_FAILURE\n");
        return HI_FALSE;
    }

    struct timeval stTimeNow;
    gettimeofday(&stTimeNow, NULL);
    HI_U64 u64TimeNowMs = stTimeNow.tv_sec * 1000 + stTimeNow.tv_usec / 1000;
    HI_S32 s32Idx = 0;
    for(s32Idx = 0; s32Idx < s_stDetectMngCtx.stMngCfg.s32ClassNum; s32Idx++)
    {
        if (s_stDetectMngCtx.pastInfo[s32Idx].s32Class == s32Class)
        {
            if ((u64TimeNowMs - s_stDetectMngCtx.pastInfo[s32Idx].u64DetectTime) > s_stDetectMngCtx.stMngCfg.s32DetectIntervalMs)
            {
                s_stDetectMngCtx.pastInfo[s32Idx].u64DetectTime = u64TimeNowMs;
                return HI_TRUE;
            }
            else
            {
                return HI_FALSE;
            }
        }
    }

    LOGE("Can`t find class[%d]\n", s32Class);
    return HI_FALSE;
}

HI_S32 PLUG_DETECTMNG_Deinit(HI_VOID)
{
    if(!s_stDetectMngCtx.bInit)
    {
        LOGI("DetectMng isn`t inited, return HI_SUCCESS\n");
        return HI_SUCCESS;
    }

    if(HI_NULL != s_stDetectMngCtx.pastInfo)
    {
        free(s_stDetectMngCtx.pastInfo);
    }
    s_stDetectMngCtx.bInit = HI_FALSE;
    return HI_SUCCESS;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* End of #ifdef __cplusplus */

